/// @tags: Offline
#include <algorithm>
#include <cctype>
#include <cstdio>
#include <cstring>

namespace BlueQuantum {

int const N = 2e5 + 5;

template <typename T>
inline T &read(T &x) {
  x = 0;
  bool f = false;
  short ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-') f = true;
    ch = getchar();
  }
  while (isdigit(ch)) x = x * 10 + (ch ^ '0'), ch = getchar();
  if (f) x = -x;
  return x;
}

struct Edge {
  int v, nxt;
} e[N];

int n, m, e_cnt, tot;
int heads[N], opt[N], a[N], b[N], ans[N];
int fa[N << 1], siz[N << 1];

inline void add(int u, int v) { e[++e_cnt].v = v, e[e_cnt].nxt = heads[u], heads[u] = e_cnt; }

int find(int x) { return !fa[x] ? x : find(fa[x]); }

void solve(int u) {
  if (opt[u] == 1) {
    int faa = find(a[u]), fab = find(b[u]);
    if (siz[faa] > siz[fab]) std::swap(faa, fab);
    if (faa != fab) { fa[faa] = fab, siz[fab] += siz[faa]; }
    for (int i = heads[u]; i; i = e[i].nxt) solve(e[i].v);
    if (faa != fab) { siz[fab] -= siz[faa], fa[faa] = 0; }
  }
  if (opt[u] == 2)
    for (int i = heads[u]; i; i = e[i].nxt) solve(e[i].v);
  if (opt[u] == 3) {
    ans[u] = find(a[u]) == find(b[u]);
    for (int i = heads[u]; i; i = e[i].nxt) solve(e[i].v);
  }
}

inline int main() {
  read(n), read(m);
  for (int i = 1; i <= n; ++i) siz[i] = 1;
  tot = n;
  for (int i = 1; i <= m; ++i) {
    read(opt[i]), read(a[i]);
    if (opt[i] & 1) read(b[i]), add(i - 1, i);
    else
      add(a[i], i);
  }
  opt[0] = 2;
  solve(0);
  for (int i = 1; i <= m; ++i)
    if (opt[i] == 3) puts(ans[i] ? "1" : "0");
  return 0;
}

}  // namespace BlueQuantum

int main() {
#ifndef ONLINE_JUDGE
#ifdef LOCAL
  freopen("/tmp/CodeTmp/testdata.in", "r", stdin);
  freopen("/tmp/CodeTmp/testdata.out", "w", stdout);
#else
  freopen("unionset.in", "r", stdin);
  freopen("unionset.out", "w", stdout);
#endif
#endif

  return BlueQuantum::main();
}